package com.kmxd.ams.app.arrange.command;

import cn.hutool.core.util.ObjectUtil;
import com.kmxd.ams.client.archive.IArchiveOrderFieldSvc;
import com.kmxd.ams.client.archive.dto.ArchiveOrderFieldQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveOrderFieldListVO;
import com.kmxd.ams.core.util.ConvertUtil;
import com.kmxd.ams.core.util.SortUtils;
import com.kmxd.ams.infra.arrange.base.SystemBO;
import com.kmxd.ams.infra.arrange.entity.Document;
import com.kmxd.ams.infra.arrange.entity.Volume;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 数据排序
 *
 * @author nickbi
 */
@Slf4j
@Component
@AllArgsConstructor
public class ArchSortCmd {

  private final IArchiveOrderFieldSvc archiveOrderFieldSvc;

  /**
   * 按文件排序规则，对数据进行排序
   *
   * @param req
   * @param list
   */
  public void sortDocumentList(SystemBO req, List<Document> list) {
    ArchiveOrderFieldQueryDTO qryOrderField = new ArchiveOrderFieldQueryDTO();
    qryOrderField.setArchTypeId(req.getArchTypeId());
    qryOrderField.setTableId(req.getTableId());
    List<ArchiveOrderFieldListVO> orderList = archiveOrderFieldSvc.selectList(qryOrderField);
    // 数据进行排序
    if (ObjectUtil.isNotEmpty(orderList)) {
      List<SortUtils.FieldOrder> fieldOrders =
          ConvertUtil.toList(orderList, SortUtils.FieldOrder.class);
      list.sort(SortUtils.buildSort(fieldOrders));
    }
  }

  /**
   * 按案卷排序规则，对数据进行排序
   *
   * @param req
   * @param list
   */
  public void sortVolumeList(SystemBO req, List<Volume> list) {
    ArchiveOrderFieldQueryDTO qryOrderField = new ArchiveOrderFieldQueryDTO();
    qryOrderField.setArchTypeId(req.getArchTypeId());
    qryOrderField.setTableId(req.getTableId());
    List<ArchiveOrderFieldListVO> orderList = archiveOrderFieldSvc.selectList(qryOrderField);
    // 数据进行排序
    if (ObjectUtil.isNotEmpty(orderList)) {
      List<SortUtils.FieldOrder> fieldOrders =
          ConvertUtil.toList(orderList, SortUtils.FieldOrder.class);
      list.sort(SortUtils.buildSort(fieldOrders));
    }
  }
}
